<?php
/**
 * Created By Project
 * Author: 陈东
 * Copyright：陈东
 * Source Code：陈东
 * Explain：I shall not authorize for any other commercial purposes, law breakers must be prosecuted.
 */
namespace Home\Controller;
use Think\Controller;
class PayController extends Controller {

	public function index(){
			if(empty($_COOKIE['user_userinfo'])){
				header("Location:http://e.hrtv.cn/ehrtv/Login/index?url=".I('post.geturl'));
			}else{
				if(empty(I('post.proid'))){
					echo "<script>alert('非法参数！');history.go(-1);</script>";
					exit();
				}
				$orderinfo = array();
				$orderinfo['geturl'] = I('post.geturl');
				$orderinfo['proid'] = I('post.proid');
				$orderinfo['money'] = I('post.money');
				$orderinfo['body'] = I('post.bodytitle');
				$orderinfo['userinfo'] = $_COOKIE['user_userinfo'];
				
				//先把数据插入数据库
				$m = M('record','pro_');
				$m->pro_id = $orderinfo['proid'];
				$m->remit_money = $orderinfo['money'];
				$m->remit_time = date("Y-m-d H:i:s", time());
				$m->remit_people = $orderinfo['userinfo'];
				$insertBakId = $m->add();
				
				if($insertBakId){
					$orderinfo['lastinsertid'] = $insertBakId;
					//支付开始
					define('APPID',C('APPID'));
					define('MCHID', C('MCHID'));
					define('KEY', C('KEY'));
					define('APPSECRET', C('APPSECRET'));
					vendor('Pay.JSAPI');
					$tools = new \JsApiPay();
					$openId = $_COOKIE['user_userinfo'];
					$Out_trade_no=date('YHis').rand(100,1000);
					//$Total_fee='测试';
					$Body = $orderinfo['body'];
					$Total_fee = $orderinfo['money']*C('MONEYMATRIXING'); //金额换算
					$input = new \WxPayUnifiedOrder();
					$input->SetBody($Body);
					$input->SetOut_trade_no($Out_trade_no);
					$input->SetTotal_fee($Total_fee);
					$input->SetNotify_url("http://h5.hrtv.cn/Home/Pay/notify");
					$input->SetTrade_type("JSAPI");
					$input->SetOpenid($openId);
					$order = \WxPayApi::unifiedOrder($input);
					$this->assign('orderinfo',$orderinfo);
					$this->assign('jsApiParameters',$tools->GetJsApiParameters($order));
					
					$this->display();
				}else{
					echo "<script>alert('数据插入失败,返回重试！');history.go(-1);</script>";
				}

			}

	}
	
	public function notify(){
		echo 1;
	}




	/*================================================================================*/
	/*来自直播页面购买门票方法*/
	public function liveindex(){
		$geturl 	=	I('post.url');
		$getmoney 	=	I('post.money');
		$liveid 	=	I('post.liveid');
		$bodystr 	=	I('post.bodystr');
		//支付成功后的回调地址
		$paysuccessbakurl = C('WEBDOMAIN') . "/Home/Pay/livenotify/";
		$this->myPayPort($getmoney,$geturl,$liveid,$bodystr,$paysuccessbakurl);
		$this->display(T('Live@Pay:index'));
	}
	public function livenotify(){
		//支付成功，操作API进行交易记录
		echo 1;
	}

	/*直播页面中，花钱换钻方法*/
	public function swopdrill(){
		$geturl 	=	I('post.url');
		$getmoney 	=	I('post.money');
		$liveid 	=	I('post.liveid');
		$bodystr 	=	I('post.bodystr');
		//支付成功后的回调地址
		$paysuccessbakurl = C('WEBDOMAIN') . "/Home/Pay/swopdnotify/";
		$this->myPayPort($getmoney,$geturl,$liveid,$bodystr,$paysuccessbakurl);
		$this->display(T('Live@Pay:swopdrill'));
	}
	public function swopdnotify(){
		//支付成功，操作API进行交易记录
		echo 1;
	}


	/*
	 * 支付接口
	 */
	protected function myPayPort($money,$bakurl,$liveid,$bodystr,$paysuccessbakurl){
		$userinfo = $_COOKIE['user_userinfo'];
		//判断用户是否与微信同步登陆
		if(empty($userinfo)){
			header("Location:".C('LOGINADDRES')."?url=".$bakurl);
		}else {
			$moneys = $money * C('MONEYMATRIXING');	//金额换算
			$payinfo = array();
			$payinfo['money'] = $moneys;			//通过换算比例得出的金额
			$payinfo['geturl'] = $bakurl;			//返回地址
			$payinfo['liveid'] = $liveid;			//直播ID号
			$payinfo['bodystr'] = $bodystr;			//支付说明
			$payinfo['userinfo'] = $userinfo;		//用户信息Token
			$payinfo['moneyinit'] = $money;			//初始金额

			//支付开始
			define('APPID', C('APPID'));
			define('MCHID', C('MCHID'));
			define('KEY', C('KEY'));
			define('APPSECRET', C('APPSECRET'));
			vendor('Pay.JSAPI');
			$tools = new \JsApiPay();
			$Out_trade_no = date('YHis') . rand(100, 1000);
			$payinfo['cordernumber'] = $Out_trade_no;
			$input = new \WxPayUnifiedOrder();
			$input->SetBody($payinfo['bodystr']);
			$input->SetOut_trade_no($payinfo['cordernumber']);
			$input->SetTotal_fee($payinfo['money']); //金额
			$input->SetNotify_url($paysuccessbakurl); //支付成功后的回调地址
			$input->SetTrade_type("JSAPI");
			$input->SetOpenid($payinfo['userinfo']);
			$order = \WxPayApi::unifiedOrder($input);

			$this->assign('payinfo', $payinfo);
			$this->assign('jsApiParameters', $tools->GetJsApiParameters($order));
		}
	}

}
?>